package com.share.pattern.observer.v2;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author Johnny Tam
 * 
 * v0.2版本 观察者模式
 * 
 */

//不论是看股票同事类， 还是看NBA的同事类，抽象出 观察者这个类
abstract class Observer{
	
	protected String name;
	protected Secretary sub;
	
	public Observer(String name,Secretary sub) {
		this.name = name;
		this.sub = sub;
	}
	
	public abstract void Update();

}

//增加具体观察者, 看股票的同事类
class StockObserver extends Observer {

	public StockObserver(String name, Secretary sub) {
		super(name, sub);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void Update() {
		// TODO Auto-generated method stub
		System.out.printf(sub.getAction() + "," + name + " 关闭股票行情，继续工作!!!\n");
	}
	
}
//增加具体观察者, 看NBA的同事类
class NBAObserver extends Observer {

	public NBAObserver(String name, Secretary sub) {
		super(name, sub);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void Update() {
		// TODO Auto-generated method stub
		System.out.printf(sub.getAction() + "," + name + " 关闭NBA视屏，继续工作!!!\n");
	}
	
}

public class Secretary {
	
	//同事列表
	private List<Observer> observers = new ArrayList<Observer>();
	private String action;
	
	//增加
	public void Attach(Observer observer) {
		observers.add(observer);
	}
	
	//通知给同事们
	public void Notify() {
		for (Observer o : observers) {
			o.Update();
		}
	}
	
	//前台状态，活动
	public String getAction() {
		return action;
	}
	public void setAction(String action) {
		this.action = action;
	}
	
	//移除同事
	public void Detach(Observer observer) {
		observers.remove(observer);
	}

}


